GameLoop = function(){

	this.frameStartTime = 0;
	this.frameEndTime = 0;
	this.frameExecTime = 0;
	this.targetFps = 25;
	this.frameDelay = 1000 / this.targetFps;
	this.enabled = true;
	this.timeout = null;
	this.$out = $('#out');

	this.currentScreen = {
		loop : function(){}
	};

	this.loadingStates = {
		'canvas': 0
	};

	this.loop = function(){
		var tmpDate = new Date();
		this.frameStartTime = (tmpDate.getSeconds()*1000)+tmpDate.getMilliseconds();
		

		this.currentScreen.loop();


		tmpDate = new Date();
		this.frameEndTime = (tmpDate.getSeconds()*1000)+tmpDate.getMilliseconds();



		if( this.frameEndTime < this.frameStartTime ) {
			this.frameStartTime -= 60000;
		}

		this.frameExecTime = this.frameEndTime - this.frameStartTime;

		if( this.frameDelay < this.frameExecTime ) {
			this.frameDelay = this.frameExecTime+1;
		} else if ( this.frameExecTime < (1000 / this.targetFps) ) {
			this.frameDelay = 1000 / this.targetFps;
		} else if ( this.frameExecTime < this.frameDelay ) {
			this.frameDelay = this.frameExecTime;
		}

		this.$out.html(this.frameDelay);
		this.timeout = window.setTimeout(function(){
				if( this.enabled ) {
					this.loop();
				}
			}.bind(this),
		this.frameDelay);
		
	};

	this.setCurrentScreen = function(screen){
		this.enabled = false;
		this.currentScreen = screen;
		this.currentScreen.init();
		this.enabled = true;
		this.loop();
	};

}